const mongoose = require('mongoose');
const shortid = require('shortid');
const moment = require('moment');


const GoodSchema = new mongoose.Schema({
    _id: {
        type: String,
        default: shortid.generate,
        unique: true//主键
    },
    name: {
        type: String,
        require: true
    },
    king_id: {
        type: String,
        ref: 'Seller'
    },
    price: {
        type: Number,
        default: 0
    },
    //商品描述
    describe: {
        type: String,
        default: '店长未添加描述...'
    },
    category: {
        type: String,
        require: true
    },
    num:{
        type:Number,
        default:1
    },
    color: {
        type: Array,
        default: []
    },
    //不同的款式
    model: {
        type: Array,
        default: []
    },
    //商品的图片们
    avatar: {
        type: Array,
        default: ['/images/1.png']
    },
    //该商品的评论
    reply: {
        type: Array,
        default: []

    },
    //上架时间
    create_time: {
        type: Date,
        default: Date.now
    },
    //被收藏量
    likeNum: {
        type: Number
    },
    //销售量
    cellNum: {
        type: Number,
        default: 0

    },
    //库存
    count: {
        type: Number,
        default: 0

    },
    //是否包邮
    send: {
        type: Boolean,
        default: true
    },
    //是否被删除
    deleted: {
        type: Boolean,
        default: false
    },
    //是否要被更改
    changeNow:{
        type:Boolean,
        default:false
},
//    给购物车判断是否选中使用
    checkReady:{
        type:Boolean,
        default:false
    }

})
GoodSchema.statics = {
    getGood: (query, callback) => {
        Good.findOne(query).then(function (good) {
            callback(null, good);
        }).catch(function (err) {
            callback(err);
        })
    },
    getGoodById: (id, callback) => {
        Good.findOne({'_id': id}).populate('king_id').then(function (good) {
            callback(null, good);
        }).catch(function (err) {
            callback(err);
        })
    },
    getGoodByName: (name, callback) => {
        Good.findOne({'name': name}).then(function (good) {
            callback(null, seller);
        }).catch(function (err) {
            callback(err);
        })
    },
    getGoodsByKing: (id,query, callback) => {

        Good.find({'king_id': id,deleted:false},query).then(function (goods) {

            callback(null, goods);
        }).catch(function (err) {

            callback(null);
        })
    },
    removeOne:(id,callback)=>{
        Good.update({'_id':id},{'deleted':true}).then((err,good)=>{
            callback(null,good);
        }).catch((err)=>{
            console.log(err);
        })
    },
    updateById: (id,data, callback) => {

        Good.update({'_id': id,deleted:false},data).then(function (good) {

            callback(null, good);
        }).catch(function (err) {

            callback(null);
        })
    },
    getGoods:(callback)=>{
        Good.find({deleted:false}).then((goods)=>{
            callback(null,goods);
        }).catch((err)=>{
            callback(err);
        })
    },
    getAllGoods:(callback)=>{
    	Good.find().then((goods)=>{
    		callback(null,goods);
    	}).catch((err)=>{
            callback(err);
        })
    }


}
GoodSchema.methods.create_time_ago = function () {
    return moment(this.create_time).fromNow();
}


const Good = mongoose.model('Good', GoodSchema);
module.exports = Good;
